home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / bindings / fun / fillpara < prev    next >
Encoding:
Text File  |  1996-09-27  |  17.4 KB  |  651 lines

  1. @if-using not(ocl-file-fillpara)
  2.   @use (ocl-file-fillpara)
  3.   ;OCL{{{}}}
  4.   ;OCL{{{  reference decription
  5.   @if-using ( FUN-REFERENCE ) ; 1 ;
  6.   This library offers the possibility, for automatic line break and block
  7.   reformating of text files.
  8.   @fi
  9.   ;OCL}}}
  10.   ;OCL{{{  libs
  11.   @if-using not(ocl-file-delspaces) @lib delspaces @fi
  12.   @use ( language-ocl )
  13.   @if-using not(ocl-file-userlang)  @lib userlang @fi
  14.   ;OCL}}}
  15.   ;OCL{{{  variables
  16.   ( defvar
  17.      ( fill-x              ; help variable
  18.        fill-count          ; help-counter
  19.        fill-block          ; 1 -> block fillling, 0 -> shrink filling
  20.        fill-col            ; last character in filled block
  21.        wrap-mode           ; should spaces break line in macro wrap-word
  22.        fill-begin          ; first char in block
  23.        fill-start          ; start of lines
  24.        fill-end            ; last char in block
  25.        fill-width          ; # of chars in block
  26.        fill-para-space-po  ; local var: position to start filling block-spaces
  27.        fill-post-c         ; local var: missing spaces for block-fill
  28.        fill-post-x         ; local var: space for block-fill found?
  29.      )
  30.   )
  31.   ;OCL}}}
  32.   ;OCL{{{  local functions
  33.   ;OCL{{{  init-fill-column
  34.   (deffun init-fill-column (if =(fill-col 0) (set fill-col 72) fi ) )
  35.   ;OCL}}}
  36.   ;OCL{{{  prompt-for-fill-column
  37.   ( deffun prompt-for-fill-column
  38.      ( begin-prompt-macro
  39.        prompt-counter fill-x ( "fill-column "  "? "   )
  40.        end-prompt-macro
  41.        if >(fill-x 0)
  42.         ( set fill-col fill-x )
  43.        fi
  44.      )
  45.   )
  46.   ;OCL}}}
  47.   ;OCL}}}
  48.   ;OCL{{{  fill-paragraph
  49.   ;OCL{{{  reference
  50.   @if-using ( FUN-REFERENCE ) ; 2 ; fill-para-called
  51.   ;OCL{{{  fill-paragraph
  52.   Fill all lines from cursor position to a end-of-block line.
  53.  
  54.   @ref-start-item end-of-block:
  55.   @ref-start-item *
  56.   A line using nother indentation
  57.   @ref-end-item
  58.   @ref-start-item *
  59.   A line startting with the current begin-fold-comment string
  60.   @ref-end-item
  61.   @ref-start-item *
  62.   If fill mode shrink and language Roff are active, a line starting with `.'.
  63.   @ref-end-item
  64.   @ref-start-item *
  65.   If fill mode shrink and language Tex are active, a line starting with `\'.
  66.   @ref-start-item *
  67.   @ref-end-item
  68.   If language OCL (User:`;OCL',`') is active, a line starting with `@'.
  69.   @ref-end-item
  70.   @ref-end-item
  71.  
  72.   If fill mode block is active, all but the last line are filled with spaces,
  73.   to match the right margin exactly. Tabs inside the filled region will be
  74.   replaced by spaces.
  75.   ;OCL}}}
  76.   @fi
  77.   ;OCL}}}
  78.   ;OCL{{{  fill-block
  79.   ;OCL{{{  fill-line-start
  80.   ( defvar ( nfls-x ) )
  81.   ( deffun fill-line-start
  82.      ( goto-counter fill-begin
  83.        if or
  84.            (
  85.              ;OCL{{{  not text
  86.              not(test-text)
  87.              ;OCL}}}
  88.              ;OCL{{{  incorrect line start
  89.              not(test-begin-line)
  90.              test-char "  ;
  91.              test-char C-I
  92.              ;OCL}}}
  93.              ;OCL{{{  special line
  94.              and
  95.               ( not(fill-block)
  96.                 or
  97.                  ( and(test-language TeX  test-char "\ )
  98.                    and(test-language Roff test-char ". )
  99.                  )
  100.               )
  101.              and(eval ( test-language-ocl ) test-char "@ )
  102.              ;OCL}}}
  103.              ;OCL{{{  starting with fold comment
  104.              and
  105.               ( fold-comment-start-length
  106.                 pre
  107.                  ( set nfls-x 0
  108.                    while
  109.                       and
  110.                        ( <(nfls-x fold-comment-start-length)
  111.                          test-char fold-comment-start nfls-x
  112.                        )
  113.                     ( set nfls-x +(nfls-x 1) )
  114.                    beginning-of-line
  115.                  )
  116.                    =(nfls-x fold-comment-start-length)
  117.               )
  118.              ;OCL}}}
  119.            )
  120.         ( return-value-from-macro false )
  121.        fi
  122.        return-value-from-macro true
  123.      )
  124.   )
  125.   ( undeclare ( nfls-x ) )
  126.   ;OCL}}}
  127.   ;OCL{{{  show-line
  128.   ( deffun show-line ( goto-counter fill-begin refresh-line ) )
  129.   ;OCL}}}
  130.   ;OCL{{{  pre-handle-line
  131.   ( deffun pre-handle-line
  132.      ( screen-off
  133.        beginning-of-line
  134.        while not(test-end-line)
  135.         ( case
  136.            ;OCL{{{  . -> maybe add space
  137.            ( and
  138.               ( not(fill-block)
  139.                 @if-using not(FORCE-DOT-SPACES)
  140.                    test-language Roff
  141.                 @fi
  142.                 test-char ".
  143.               )
  144.              ( forward-character
  145.                if or(test-char "  test-char C-I)
  146.                 ( "   ;
  147.                   forward-character
  148.                   delspaces
  149.                 )
  150.                fi
  151.              )
  152.            )
  153.            ;OCL}}}
  154.            ;OCL{{{  space/tab: leave only one
  155.            ( or(test-char "  test-char C-I)
  156.              ( "  ;
  157.                delspaces
  158.              )
  159.            )
  160.            ;OCL}}}
  161.           default
  162.            ( forward-character )
  163.           esac
  164.         )
  165.        screen-on
  166.      )
  167.   )
  168.   ;OCL}}}
  169.   ;OCL{{{  post-handle-line
  170.   ( deffun post-handle-line
  171.      ( if fill-block
  172.         ;OCL{{{  expand sline exact to fill column
  173.         ( screen-off
  174.           ;OCL{{{  fill-post-c = number of missing spaces
  175.           goto-counter fill-end
  176.           set fill-post-c 0
  177.           while and(not(test-begin-line),test-char " ) (
  178.             set fill-post-c +(fill-post-c 1)
  179.             backward-character
  180.           )
  181.           ;OCL}}}
  182.           do
  183.            ;OCL{{{  one line pass, fill-post-x=0, if space found
  184.            (
  185.              ;OCL{{{  move cursor to correct position
  186.              if =(fill-para-space-po 0)
  187.               ( set fill-post-x 1
  188.                 end-of-line
  189.               )
  190.              else
  191.               ( set fill-post-x 0
  192.                 goto-counter fill-para-space-po
  193.                 while test-end-line (backward-character)
  194.                 forward-character
  195.               )
  196.              fi
  197.              ;OCL}}}
  198.              while and(>(fill-post-c 0) not(test-begin-line)) (
  199.                ;OCL{{{  maybe insert space, go left
  200.                backward-character
  201.                if test-char "  (
  202.                  "  ;
  203.                  backward-character
  204.                  backward-character
  205.                  set fill-post-x 0
  206.                  set fill-post-c +(fill-post-c -1)
  207.                ) fi
  208.                ;OCL}}}
  209.              )
  210.              set fill-para-space-po 0
  211.            )
  212.            ;OCL}}}
  213.           while and(>(fill-post-c 0) =(fill-post-x 0))
  214.           set fill-para-space-po store-pos
  215.           screen-on
  216.         )
  217.         ;OCL}}}
  218.        fi
  219.        show-line
  220.      )
  221.   )
  222.   ;OCL}}}
  223.   ;OCL{{{  post-last-handle
  224.   ( defmac post-last-handle
  225.      ( if fill-block
  226.         ( pre-handle-line show-line )
  227.        fi
  228.      )
  229.   )
  230.   ;OCL}}}
  231.   ;OCL{{{  fill-line
  232.   ( defmac fill-line
  233.      (
  234.        ;OCL{{{  how many chars needed, which position in next line
  235.        screen-off
  236.        end-of-line
  237.        set fill-count store-pos
  238.        backward-character
  239.        if and
  240.            ( not(fill-block)
  241.              @if-using ( FORCE-DOT-SPACES )
  242.                 test-language Roff test-char ".
  243.              @fi
  244.            )
  245.         ( set fill-count +(fill-count 1) )
  246.        fi
  247.        set fill-count -(fill-end fill-count)
  248.        set fill-x +(fill-count fill-begin)
  249.        screen-on
  250.        ;OCL}}}
  251.        next-line
  252.        screen-off
  253.        goto-counter fill-x
  254.        if test-end-line
  255.         ;OCL{{{  next line to short
  256.         ( goto-counter fill-start
  257.           delspaces
  258.           screen-on
  259.           delete-previous-character
  260.           screen-off
  261.           if and
  262.               ( not(fill-block)
  263.                 @if-using ( FORCE-DOT-SPACES )
  264.                    test-language Roff
  265.                 @fi
  266.               )
  267.            ( backward-character
  268.              if test-char ". (forward-character " ) else (forward-character) fi
  269.            )
  270.           fi
  271.           "  ;
  272.           screen-on
  273.         )
  274.         ;OCL}}}
  275.        else
  276.         ;OCL{{{  line long
  277.         (
  278.           ;OCL{{{  search splitt positions
  279.           while and(>(fill-count 0) not(test-char " ))
  280.            ( backward-character
  281.              set fill-count +(fill-count -1)
  282.            )
  283.           ;OCL}}}
  284.           screen-on
  285.           if >(fill-count 0)
  286.            ;OCL{{{  can splitt
  287.            ( newline-and-indent
  288.              screen-off
  289.              goto-counter fill-begin
  290.              delspaces
  291.              screen-on
  292.              previous-line
  293.              screen-off
  294.              goto-counter fill-start
  295.              delspaces
  296.              screen-on
  297.              delete-previous-character
  298.              screen-off
  299.              if and
  300.                  ( not(fill-block)
  301.                    @if-using ( FORCE-DOT-SPACES )
  302.                       test-language Roff
  303.                    @fi
  304.                  )
  305.               ( backward-character
  306.                 if test-char ".
  307.                  ( forward-character "  )
  308.                 else
  309.                  ( forward-character )
  310.                 fi
  311.               )
  312.              fi
  313.              "  ;
  314.              screen-on
  315.              next-line
  316.            )
  317.            ;OCL}}}
  318.           fi
  319.           previous-line
  320.           post-handle-line
  321.           next-line
  322.         )
  323.         ;OCL}}}
  324.        fi
  325.      )
  326.   )
  327.   ;OCL}}}
  328.   ;OCL{{{  splitt-line
  329.   ( defmac splitt-line
  330.      ( screen-off
  331.        goto-counter fill-end
  332.        forward-character
  333.        set fill-count fill-width
  334.        ;OCL{{{  look for a splitt-position
  335.        while and(>(fill-count 0) not(test-char " ) not(test-char C-I))
  336.         ( backward-character
  337.           set fill-count +(fill-count -1)
  338.         )
  339.        screen-on
  340.        if =(fill-count 0)
  341.         ( return-from-macro )
  342.        fi
  343.        ;OCL}}}
  344.        newline-and-indent
  345.        screen-off
  346.        goto-counter fill-begin
  347.        delspaces
  348.        screen-on
  349.      )
  350.   )
  351.   ;OCL}}}
  352.   ;OCL{{{  fill-loop
  353.   ( defmac fill-loop
  354.      ( while eval ( fill-line-start )
  355.         ( goto-counter fill-end
  356.           forward-character
  357.           if test-end-line
  358.            ;OCL{{{  fill or leave
  359.            ( backward-character
  360.              if test-end-line
  361.               ;OCL{{{  fill it
  362.               ( next-line
  363.                 if not( eval ( fill-line-start ) )
  364.                  ;OCL{{{  => ready
  365.                  ( previous-line
  366.                    post-handle-line
  367.                    show-line
  368.                    next-line
  369.                    beginning-of-line
  370.                    return-from-macro
  371.                  )
  372.                  ;OCL}}}
  373.                 fi
  374.                 pre-handle-line
  375.                 previous-line
  376.                 fill-line
  377.               )
  378.               ;OCL}}}
  379.              else
  380.               ;OCL{{{  line exact ok
  381.               ( post-handle-line
  382.                 next-line
  383.                 pre-handle-line
  384.               )
  385.               ;OCL}}}
  386.              fi
  387.            )
  388.            ;OCL}}}
  389.           else
  390.            ;OCL{{{  splitting
  391.            ( splitt-line
  392.              previous-line
  393.              post-handle-line
  394.              next-line
  395.            )
  396.            ;OCL}}}
  397.           fi
  398.         )
  399.        show-line
  400.      )
  401.   )
  402.   ;OCL}}}
  403.  
  404.   ( defmac do-fill-paragraph
  405.      (
  406.        ;OCL{{{  set start/begin/end/fillpo/width
  407.        ; fill-begin = first char in block
  408.        ; fill-start = first edit-position in current fold
  409.        ; fill-end   = fill-column
  410.        ; fill-width = number of chars in a blockline
  411.        ; fill-para-space-po = 0
  412.        ;              counter to spread added spaces (block-mode)
  413.        beginning-of-line
  414.        set fill-begin store-pos
  415.        newline-and-indent
  416.        previous-line
  417.        beginning-of-line
  418.        set fill-start store-pos
  419.        delete-line
  420.        init-fill-column
  421.        set fill-end fill-col
  422.        set fill-width +(1 -(fill-end fill-begin))
  423.        set fill-para-space-po 0
  424.        ;OCL}}}
  425.        ;OCL{{{  check start line
  426.        if not(eval ( fill-line-start ))
  427.         ( message ( M_ERR_PO )
  428.           return-from-macro
  429.         )
  430.        fi
  431.        ;OCL}}}
  432.        pre-handle-line
  433.        fill-loop
  434.        previous-line
  435.        post-last-handle
  436.      )
  437.   )
  438.   ;OCL}}}
  439.  
  440.   ( deffun fill-paragraph
  441.      ( case
  442.         ( in-prompt () )
  443.         ( test-overwrite ( message ( "Overwrite "! ) ) )
  444.         ( not(test-text) ( message ( M_ERR_PO ) ) )
  445.        default
  446.         ( init-fill-column
  447.           do-fill-paragraph
  448.         )
  449.        esac
  450.      )
  451.   )
  452.   ;OCL}}}
  453.   ;OCL{{{  set-fill-column
  454.   ;OCL{{{  reference
  455.   @if-using ( FUN-REFERENCE ) ; 2 ; set-col-called
  456.   ;OCL{{{  set-fill-column
  457.   Define the right margin for fill-paragraph. The value is given by:
  458.  
  459.   @ref-start-item
  460.   modify-behaviour, if greater then 0,
  461.   @ref-end-item
  462.   @ref-start-item or
  463.   by the value of repeat-argument,
  464.   @ref-end-item
  465.   @ref-start-item or
  466.   the cursor x position, if greater than 20,
  467.   @ref-end-item
  468.   @ref-start-item or
  469.   by your input (set-fill-column prompts for).
  470.   @ref-end-item
  471.   ;OCL}}}
  472.   @fi
  473.   ;OCL}}}
  474.   (deffun set-fill-column
  475.     ( init-fill-column
  476.       set fill-x read-repeat
  477.       case
  478.        ( >(modify-behaviour 0) ( set fill-col modify-behaviour ) )
  479.        ( >(fill-x 0)     ( set fill-col fill-x ) )
  480.        ( >(store-pos 20) ( set fill-col store-pos ) )
  481.       default
  482.        ( prompt-for-fill-column )
  483.       esac
  484.     )
  485.   )
  486.   ;OCL}}}
  487.   ;OCL{{{  set-fill-mode
  488.   ;OCL{{{  reference
  489.   @if-using ( FUN-REFERENCE ) ; 2 ; set-mode-called
  490.   ;OCL{{{  set-fill-column
  491.   Offers a menu, for selecting the fill mode (shrink or block) and the right
  492.   fill margin.
  493.   ;OCL}}}
  494.   @fi
  495.   ;OCL}}}
  496.   ( deffun set-fill-mode
  497.      ( set fill-x fill-block
  498.        init-fill-column
  499.        begin-prompt-macro
  500.        menu-counter
  501.           fill-x
  502.         ( "Shrink "  "Block "  "Column "( counter fill-col ") )
  503.        end-prompt-macro
  504.        if =(fill-x 2)
  505.         ( prompt-for-fill-column )
  506.        else
  507.         ( set fill-block fill-x )
  508.        fi
  509.      )
  510.   )
  511.   ;OCL}}}
  512.   ;OCL{{{  wrap
  513.   ( demand-load not
  514.      (
  515.         ;OCL{{{  wrap-word
  516.         ;OCL{{{  reference
  517.         @if-using ( FUN-REFERENCE ) ; 2 ; wrap-called
  518.         ;OCL{{{  wrap-word
  519.         Can be used to replace the single space.
  520.  
  521.         If used at end of a line, behind the right fill margin, the last word
  522.         will be moved to the next line and a space will be added.
  523.  
  524.         In front of fill margin and if mode wrap is not active, a single space
  525.         will be added.
  526.         ;OCL}}}
  527.         @fi
  528.         ;OCL}}}
  529.         ( deffun wrap-word
  530.            ( if
  531.               ;OCL{{{  word should be wrapped
  532.                 and
  533.                  ( >(wrap-mode 0)
  534.                    not(in-prompt)
  535.                    test-end-line
  536.                    pre ( init-fill-column ) >(store-pos fill-col)
  537.                  )
  538.               ;OCL}}}
  539.               ;OCL{{{  wrap the word
  540.               (
  541.                 ;OCL{{{  go to last char in line
  542.                 while and(test-char "  not(test-begin-line))
  543.                  ( backward-character )
  544.                 ;OCL}}}
  545.                 ;OCL{{{  move to correct wrap-position
  546.                 if >(store-pos fill-col)
  547.                  ;OCL{{{  wrap word
  548.                  (
  549.                    ;OCL{{{  move to last space
  550.                    while and(not(test-char " ) not(test-begin-line))
  551.                     ( backward-character )
  552.                    ;OCL}}}
  553.                    ;OCL{{{  if not at start-of-line, right to correct wrap-word
  554.                    if test-begin-line ( end-of-line ) else ( forward-character ) fi
  555.                    ;OCL}}}
  556.                  )
  557.                  ;OCL}}}
  558.                 else
  559.                  ( forward-character )
  560.                 fi
  561.                 ;OCL}}}
  562.                 newline-and-indent
  563.                 ;OCL{{{  maybe move behind wraped word
  564.                 previous-line
  565.                 beginning-of-line
  566.                 next-line
  567.                 if not(test-end-line)
  568.                  ( end-of-line "  )
  569.                 fi
  570.                 ;OCL}}}
  571.               )
  572.               ;OCL}}}
  573.              else
  574.               ;OCL{{{  insert space
  575.               ( "  )
  576.               ;OCL}}}
  577.              fi
  578.            )
  579.         )
  580.         ;OCL}}}
  581.         ;OCL{{{  add/delete wrap-mode
  582.         (defmodestring WM "Wrap "W )
  583.         ;OCL{{{  add-mode-wrap
  584.         ;OCL{{{  reference
  585.         @if-using ( FUN-REFERENCE ) ; 2 ; add-wrap-called
  586.         ;OCL{{{  add-mode-wrap
  587.         Activate automatic line wrapping. Mark the mode with `Wrap' or `W' in
  588.         the statusline.
  589.         ;OCL}}}
  590.         @fi
  591.         ;OCL}}}
  592.         (deffun add-mode-wrap (
  593.           if in-prompt ( return-from-macro ) fi
  594.           set wrap-mode 1 set-user-mode WM
  595.         ))
  596.         ;OCL}}}
  597.         ;OCL{{{  delete-mode-wrap
  598.         ;OCL{{{  reference
  599.         @if-using ( FUN-REFERENCE ) ; 2 ; delete-wrap-called
  600.         ;OCL{{{  delete-mode-wrap
  601.         Switch of automatic line wrapping.
  602.         ;OCL}}}
  603.         @fi
  604.         ;OCL}}}
  605.         (deffun delete-mode-wrap (
  606.           if in-prompt ( return-from-macro ) fi
  607.           set wrap-mode 0 reset-user-mode WM
  608.         ))
  609.         ;OCL}}}
  610.         ;OCL}}}
  611.      )
  612.   )
  613.   ;OCL}}}
  614.   ;OCL{{{  undeclare local data
  615.   ( undeclare
  616.      ( fill-block
  617.        fill-col
  618.        prompt-for-fill-column
  619.        show-line
  620.        pre-handle-line
  621.        post-handle-line
  622.        post-last-handle
  623.        fill-line
  624.        splitt-line
  625.        fill-loop
  626.        do-fill-paragraph
  627.        fill-line-start
  628.        fill-begin
  629.        fill-start
  630.        fill-end
  631.        fill-width
  632.        fill-para-space-po
  633.        fill-post-c
  634.        fill-post-x
  635.        fill-count
  636.        fill-x
  637.      )
  638.   )
  639.   ;OCL}}}
  640.   ;OCL{{{  reference hooks
  641.   @if-using ( FUN-REFERENCE ) ; 3 ;
  642.   The library uses begin-prompt-macro and end-prompt-macro.
  643.  
  644.   Defining the Tag FORCE-DOT-SPACES will always use 2 spaces after `.',
  645.   if block filling is not active.
  646.  
  647.   The wrap-word macros are delared as demand-loading.
  648.   @fi
  649.   ;OCL}}}
  650. @fi
  651.